<?php

namespace app\api\controller;

use think\Controller;
use think\Request;
use think\Db;
use app\model\ProductOrder;

class Product extends controller
{
    protected $middleware = [
        'check' => [
            'only' => ['buy']
        ]
    ];

    public function getProductList(Request $request)
    {
        $page = $request->get('page', 1, 'intval');
        $page = $page > 0 ? $page : 1;
        $course = Db('product');
        $list = $course
            ->order('product_id asc')
            ->page($page, 10)
            ->field('product_id,name,image,price,money,num,deposit,description')
            ->select();
        $token = $request->param('token');
        foreach ($list as $key => $val) {
            if ($token && $user = Db('user')->where('token', $token)->field('token', true)->find()) {
                $product_ids = Db('productOrder')->where('user_id', $user['user_id'])->where('status', 1)->column('product_id');
                if (in_array($val['product_id'], $product_ids)) {
                    $list[$key]['is_buy'] = 1;
                } else {
                    $course[$key]['is_buy'] = 0;
                }
            } else {
                $course[$key]['is_buy'] = 0;
            }
            $course[$key]['product_num'] = floor($val['num'] / 2);
        }
        return ['code' => 0, 'data' => $list];
    }

    public function getProductInfo(Request $request)
    {
        $product_id = $request->get('product_id', 0, 'intval');
        $info = Db('product')->where('product_id', $product_id)->find();
        if (!$info) {
            return ['code' => 2, 'msg' => '套餐已下架'];
        }
        $token = $request->param('token');
        if ($token && $user = Db('user')->where('token', $token)->field('token', true)->find()) {
            $product_ids = Db('productOrder')->where('user_id', $user['user_id'])->where('status', 1)->column('product_id');
            if (in_array($product_id, $product_ids)) {
                $info['is_buy'] = 1;
            } else {
                $info['is_buy'] = 0;
            }
        }
        $info['product_num'] = floor($info['num'] / 2);
        return ['code' => 0, 'data' => $info];
    }

    public function buy(Request $request)
    {
        $product_id = $request->post('product_id', 0, 'intval');
        $course_id = $request->post('course_id', 0, 'intval');
        Db::startTrans();
        $product = Db('product')->where('product_id', $product_id)->lock(true)->find();
        if (!$product) {
            Db::rollback();
            return ['code' => 3, 'msg' => '套餐已下架'];
        }
        $productOrder = Db('productOrder')->where('product_id', $product_id)->where('user_id', $request->user['user_id'])->where('status', 'gt', 0)->find();
        if ($productOrder) {
            Db::rollback();
            return ['code' => 3, 'msg' => '请勿重复购买'];
        }
        $order = Db('order')->where('course_id', $course_id)->where('user_id', $request->user['user_id'])->where('status', 1)->find();
        if (!$order) {
            Db::rollback();
            return ['code' => 3, 'msg' => '未购买课程'];
        }
        $course = Db('course')->where('course_id', $course_id)->find();
        if (!$course) {
            Db::rollback();
            return ['code' => 3, 'msg' => '课程已下架'];
        }
        $time = time();
        if ($product['type'] == 1 && ($course['product_start_time'] > $time || $course['product_end_time'] < $time)) {
            Db::rollback();
            return ['code' => 3, 'msg' => '不在抢购时间'];
        }

        if ($product['type'] == 2){
            if($course['server_start_time'] > $time || $course['server_end_time'] < $time) {
                Db::rollback();
                return ['code' => 3, 'msg' => '不在抢购时间'];
            }
            if(Db('productOrder')->where('product_id', $product_id)->where('user_id', $request->user['user_id'])->where('status', 'gt', 0)->count() > $product['num']){
                Db::rollback();
                return ['code' => 3, 'msg' => '抢购人数已满'];
            }
        }

        $transaction = ProductOrder::getTransaction();
        $order_data = [
            'user_id' => $request->user['user_id'],
            'transaction' => $transaction,
            'product_id' => $product_id,
            'course_id' => $course_id,
            'price' => $product['price'],
            'product_price' => $product['price'],
            'product_name' => $product['name'],
            'product_image' => $product['image'],
            'create_time' => date('Y-m-d H:i:s'),
            'status' => 0
        ];
        $order_id = Db('productOrder')->insert($order_data, false, true);
        if ($order_id) {
            Db::commit();
            return ['code' => 0, 'data' => ['order_id' => $order_id]];
        } else {
            Db::rollback();
            return ['code' => 2, 'msg' => '下单失败'];
        }
    }

}